package com.voxel.sense.infomsg.module.map;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.example.com.common.app.BaseActivity;
import com.example.com.common.utils.AppUtils;
import com.example.com.common.utils.ToastUtil;
import com.example.com.common.view.dialog.DialogCallBack;
import com.example.com.common.view.dialog.IDialog;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.utils.GPSUtils;
import com.voxel.sense.infomsg.utils.MapIntentUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cyy.example.com.facotry.constants.IntentConstants;

public class MapActivity extends BaseActivity {


    @BindView(R.id.map_view)
    MapView map_view;
    @BindView(R.id.tv_area)
    TextView tv_area;
    @BindView(R.id.tv_navigation)
    TextView tv_navigation;
    private AMap aMap;//地图操控类
    private LatLng mLng; //指定定位点的位置 search 坐标
    private MarkerOptions mMarkerOption;
    private Marker mMarker;
    private String mAresDes;
    private double mLatitude;
    private double mLongitude;


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_map;
    }


    public static void show(Context context, double latitude, double longitude) {
        show(context, "", latitude, longitude);
    }


    public static void show(Context context, String areaDes, double latitude, double longitude) {
        Intent intent = new Intent(context, MapActivity.class);
        intent.putExtra(IntentConstants.LATITUDE, latitude);
        intent.putExtra(IntentConstants.LONGITUDE, longitude);
        intent.putExtra(IntentConstants.STRING_TYPE, areaDes);
        context.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        map_view.onCreate(savedInstanceState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        map_view.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        map_view.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        map_view.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        map_view.onDestroy();
    }


    @Override
    protected boolean initArgs(Bundle bundle) {
        mLatitude = bundle.getDouble(IntentConstants.LATITUDE, 0);
        mLongitude = bundle.getDouble(IntentConstants.LONGITUDE, 0);
        mAresDes = bundle.getString(IntentConstants.STRING_TYPE);
        if (mLatitude != 0 && mLongitude != 0) {
            mLng = new LatLng(mLatitude, mLongitude, true);
        }

        return super.initArgs(bundle);
    }

    @Override
    protected void initData() {
        super.initData();
        if (aMap == null) {
            aMap = map_view.getMap();
        }
        mMarkerOption = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_marker_firm))
                .position(mLng)
                .draggable(true);
        AMapOptions aMapOptions = new AMapOptions();
        mMarker = aMap.addMarker(mMarkerOption);
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
        aMap.moveCamera(CameraUpdateFactory.changeLatLng(mLng));
        aMap.getUiSettings().setZoomControlsEnabled(false);
        if (TextUtils.isEmpty(mAresDes)) {
            tv_navigation.setVisibility(View.GONE);
            tv_area.setVisibility(View.GONE);
        } else {
            tv_navigation.setVisibility(View.VISIBLE);
            tv_area.setVisibility(View.VISIBLE);
            tv_area.setText(mAresDes);
        }
    }

    @Override
    protected void initEven() {
        super.initEven();
        tv_navigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List list = new ArrayList();
                list.add("高德地图");
                list.add("百度地图");
                list.add("腾讯地图");
                IDialog.getInstance().showItemDialog(MapActivity.this, "", list, new DialogCallBack() {
                    @Override
                    public void onClick(int what) {

                        if (what == 0) {
                            //跳转高德
                            GoToMapNavigation(MapIntentUtils.getGaoDeMapIntent(mAresDes, mLatitude, mLongitude), "com.autonavi.minimap", "高德");
                        } else if (what == 1) {
                            //跳转百度
                            double[] doubles = GPSUtils.gcj02_To_Bd09(mLatitude, mLongitude);
                            GoToMapNavigation(MapIntentUtils.getBaiDuMapIntent(mAresDes, doubles[0], doubles[1]), "com.baidu.BaiduMap", "百度");
                        } else if (what == 2) {
                            //腾讯
                            GoToMapNavigation(MapIntentUtils.getTXMapIntent(mAresDes, mLatitude, mLongitude), "com.tencent.map", "腾讯");
                        }
                    }
                });
            }
        });
    }


    private void GoToMapNavigation(Intent intent, String pageName, String des) {

        if (!TextUtils.isEmpty(pageName) && intent != null) {
            //先判断是否有安装
            if (AppUtils.isInstallApp(pageName)) {
                try {
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                ToastUtil.showToastLong(this, String.format(getResources().getString(R.string.map_no_install), des));
                Uri uri = Uri
                        .parse("market://details?id=" + pageName);
                Intent intentInstall = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intentInstall);
            }
        }
    }

}
